package cn.aigoubuy.service.impl;

import cn.aigoubuy.dao.ProductDao;
import cn.aigoubuy.dao.ilpl.ProductDaoImpl;
import cn.aigoubuy.domain.PageBean;
import cn.aigoubuy.domain.Product;
import cn.aigoubuy.service.ProductService;

import java.sql.SQLException;
import java.util.List;

public class ProductServiceImpl implements ProductService {
    private ProductDao productdao = new ProductDaoImpl();
    @Override
    public List<Product> findByHot() throws SQLException {
        return productdao.findByHot();
    }
    @Override
    public List<Product> findByNew() throws SQLException{
        return productdao.findByNew();
    }
    @Override
    public Product findById(String pid) throws SQLException {
        return productdao.findById(pid);
    }
    @Override
    public PageBean<Product> findByCid(String cid, int pageNumber, int pageSize) throws SQLException {
        // 1.获得总记录数
        int totalRecord = productdao.findTotalRecordByCid(cid);
        // 2.封装数据
        PageBean<Product> pageBean = new PageBean<>(pageNumber,totalRecord,pageSize);
        //设置url
        pageBean.setUrl("productServlet?method=findByCid&cid="+cid);
        // 3.分页数据
        List<Product> data = productdao.findAllByCid(cid,pageBean.getStartIndex(),pageBean.getPageSize());
        pageBean.setList(data);
        return pageBean;
    }
}